package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//解决跨域问题
@CrossOrigin
@Tag(name = "公告管理接口")
@RestController
@RequestMapping("/api/notices")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    @Operation(summary = "获取所有公告列表")
    @GetMapping("/list")
    public Result<List<Notice>> list() {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照排序字段倒序排序(优先级高在前)
        noticeLambdaQueryWrapper.orderByDesc(Notice::getPriority);
        //调用NoticeService中的list方法
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取最新公告列表")
    @GetMapping("/latest")
    public Result<List<Notice>> latest(@Parameter(description = "限制数量", example = "5") @RequestParam(defaultValue = "5") int pageNum) {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //通过更新时间排序(时间越新越靠前)
        noticeLambdaQueryWrapper.eq(Notice::getIsActive, true)
                .orderByDesc(Notice::getUpdateTime)
                .last("limit " + pageNum);
        //调用NoticeService中的list方法
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        //返回最新公告列表
        return Result.success(list);

    }

    @Operation(summary = "根据ID获取公告")
    @GetMapping("/{id}")
    public Result<Notice> getById(@PathVariable Long id) {
        //调用NoticeService中的getById方法
        Notice notice = noticeService.getById(id);
        return notice != null ? Result.success(notice) : Result.error("公告不存在");
    }

    @Operation(summary = "获取启用的公告")
    @GetMapping("/active")
    public Result activeList() {
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.eq(Notice::getIsActive, true);
        //调用NoticeService中的list方法
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "发布新公告")
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        //调用NoticeService中的save方法
        boolean save = noticeService.save(notice);
        return save ? Result.success("添加成功") : Result.error("添加失败");
    }

    @Operation(summary = "更新公告")
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        //调用NoticeService中的updateById方法
        boolean update = noticeService.updateById(notice);
        return update ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        //调用NoticeService中的removeById方法
        boolean remove = noticeService.removeById(id);
        return remove ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "切换公告激活状态")
    @PutMapping("/toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam Boolean isActive) {
        //创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置要跟新的字段
        noticeLambdaUpdateWrapper.set(Notice::getIsActive, isActive).set(Notice::getUpdateTime, new Date());
        //设置查询条件
        noticeLambdaUpdateWrapper.eq(Notice::getId, id);
        //调用NoticeService中的update方法
        boolean update = noticeService.update(noticeLambdaUpdateWrapper);
        return update ? Result.success("切换公告激活状态成功") : Result.error("切换公告激活状态失败");
    }


}
